<script>
import request from "../../utils/Request.js";
import store from "../../store/index.js";
import {ElMessage, ElMessageBox} from "element-plus";
import router from "../../router/index.js";

export default {
  methods: {
    notify() {
      ElMessageBox.confirm(
          '服务器出现未知错误，您可以选择退出登录，但是这无法清除您在服务器的登录信息，不会影响下次登录。您也可以选择稍后再试。',
          '退出登录失败',
          {
            confirmButtonText: '退出登录',
            cancelButtonText: '稍后再试',
            type: 'warning',
          }
      )
          .then(() => {
            // 再次进行尝试
            request({
              method: 'post',
              url: '/api/uaa/logout'
            })
            // 直接清除
            store.commit('clearAllUserData')
            ElMessage.success('退出登录成功！');
            router.push('/')
          })
          .catch(() => router.back())
    }
  },
  beforeCreate() {
    request({
      method: 'post',
      url: '/api/uaa/logout'
    })
        .then(resp => {
          let data = resp.data
          if (data.code === 0) {
            store.commit('clearAllUserData')
            ElMessage.success('退出登录成功！');
            router.push('/')
          } else {
            this.notify()
          }
        })
        .catch(err => {
          console.log(err)
          this.notify()
        })
  }
}
</script>

<template>
  <div class="outer">
    <h2>正在退出登录</h2>
    <el-progress :percentage="100" :show-text="false" indeterminate :stroke-width="8"/>
  </div>
</template>

<style>
.outer {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>